package com.epam.qanda.db.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.epam.qanda.db.ConnectionManager;
import com.epam.qanda.db.dao.BaseDao;
import com.epam.qanda.db.mapping.Converter;
import com.epam.qanda.db.model.Answer;

public class AnswerDao implements BaseDao {
	private final String ADD_ANSWER = "INSERT INTO answer (question_id, user_id, text,time, likes) VALUES (?,?,?,?,?)";
	private final String REMOVE_ANSWER = "DELETE FROM answer WHERE id = ?";
	private final String GET_ANSWER_BY_ID = "SELECT * FROM answer WHERE id = ?";
	private final String GET_ALL_ANSWERS = "SELECT * FROM answer WHERE question_id = ?";
	private final String UPDATE_ANSWER = "UPDATE answer SET question_id?, user_id=?, text=?, likes=?, time=? WHERE id = ?";
	private final String UPDATE_LIKES = "UPDATE answer SET likes=? WHERE id = ?";

	private static final Logger LOG = Logger.getLogger(AnswerDao.class);
	
	@Override
	public <T> void create(T element) {
		Answer temp = (Answer) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(ADD_ANSWER)) {
			preparedStatement.setInt(1, temp.getQuestionId());
			preparedStatement.setInt(2, temp.getUserId());
			preparedStatement.setString(3, temp.getText());
			preparedStatement.setTimestamp(4, temp.getTime());
			preparedStatement.setInt(5, temp.getLikes());
			preparedStatement.executeUpdate();
			LOG.info("answer added");
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T read(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		Answer answer = null;
		try (PreparedStatement statement = connection
				.prepareStatement(GET_ANSWER_BY_ID)) {
			statement.setInt(1, id);
			ResultSet rs = statement.executeQuery();
			if (rs.next())
				answer = Converter.<Answer> fromResaultSetToObject(rs,
						Answer.class);
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) answer;

	}

	@Override
	public void delete(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(REMOVE_ANSWER)) {
			preparedStatement.setInt(1, id);
			preparedStatement.executeUpdate();
			LOG.info("Answer deleted");
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@Override
	public <T> void update(T element) {
		Answer temp = (Answer) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(UPDATE_ANSWER)) {
			preparedStatement.setInt(6, temp.getId());
			preparedStatement.setInt(1, temp.getQuestionId());
			preparedStatement.setInt(2, temp.getUserId());
			preparedStatement.setString(3, temp.getText());
			preparedStatement.setTimestamp(4, temp.getTime());
			preparedStatement.setInt(5, temp.getLikes());
			preparedStatement.executeUpdate();
			LOG.info("Answer updated");
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}

	}

	@SuppressWarnings("unchecked")
	public <T> T getAllAnswers(Integer answerId) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		List<Answer> answers = new ArrayList<Answer>();
		try (PreparedStatement statement = connection
				.prepareStatement(GET_ALL_ANSWERS)) {
			statement.setInt(1, answerId);
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				answers.add(Converter.<Answer> fromResaultSetToObject(rs,
						Answer.class));
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) answers;
	}

	public <T> void updateLikes(T element) {
		Answer temp = (Answer) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(UPDATE_LIKES)) {
			preparedStatement.setInt(2, temp.getId());
			preparedStatement.setInt(1, temp.getLikes());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}

	}

}
